package com.ebc.eservice.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ebc.eservice.mapper.SingleIntroductionMapper;
import com.ebc.eservice.mapper.UserMapper;
import com.ebc.eservice.pojo.EbcUser;
import com.ebc.eservice.pojo.SingleIntroduction;
import com.ebc.eservice.pojo.pic.UploadFile;
import com.ebc.eservice.service.EbcIntroductService;
import com.ebc.eservice.util.JsonResult;
import com.ebc.eservice.vo.Intro.IntroductionReqVo;
import com.ebc.eservice.vo.Intro.IntroductionUpdateVo;
import org.bson.types.Binary;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.Date;
import java.util.List;

/**
 * @author: Joey
 * @Description:个性化简介-后台
 * @date:2022/9/26 23:04
 */
@Service
public class IntroductionImp implements EbcIntroductService {

    @Autowired
    private SingleIntroductionMapper IntroductionMapper;
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private MongoTemplate mongoTemplate;

    @Override
    public JsonResult getIntroductionList(int current, int size) {
        Page<SingleIntroduction> page = new Page<>(current, size);
        QueryWrapper<SingleIntroduction> queryWrapper = new QueryWrapper<>();
        //todo 个性化简介 目前暂时设计 一个公司
        queryWrapper.eq("company_id",1);
//        queryWrapper.eq("open",1);  //后台不用显示模块的显示
        IntroductionMapper.selectPage(page,queryWrapper);
        List<SingleIntroduction> list = page.getRecords();
        return JsonResult.success(list);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public JsonResult addIntroduction(IntroductionReqVo introReq) {
        SingleIntroduction introduction = new SingleIntroduction();

        EbcUser user = userMapper.selectById(introReq.getCurrentId());

        introduction.setOpen(1).setCreator(user.getName())
                .setTypeName(introReq.getTypeName())
                .setCompanyId(introReq.getCompanyId()).setContent(introReq.getContent());
        IntroductionMapper.insert(introduction);
        return JsonResult.success("添加成功");
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public JsonResult addIntroPic(Integer IntroId, MultipartFile IntroFile) throws IOException {
        JsonResult result=null;
        UploadFile uploadFile = new UploadFile();
        Query query = new Query(Criteria.where("introId").is(IntroId));
        UploadFile file = mongoTemplate.findOne(query,UploadFile.class);
        if(file!=null){ //更新
            file.setContent(new Binary(IntroFile.getBytes()));
            file.setName(IntroFile.getOriginalFilename());
            file.setUpdateTime(new Date());
            file.setIntroId(IntroId);
            file.setContentType(IntroFile.getContentType());
            file.setSize(IntroFile.getSize());
            mongoTemplate.save(file);
            result = JsonResult.success("简介id-> "+IntroId+",照片更新");
        }else{  //新增
            uploadFile.setContent(new Binary(IntroFile.getBytes()));
            uploadFile.setName(IntroFile.getOriginalFilename());
            uploadFile.setCreatedTime(new Date());
            uploadFile.setIntroId(IntroId);//introId
            uploadFile.setContentType(IntroFile.getContentType());
            uploadFile.setSize(IntroFile.getSize());
            mongoTemplate.save(uploadFile);
            result = JsonResult.success("简介id-> "+IntroId+",照片新增");
        }
        return result;
    }

    @Override
    public JsonResult updateOpen(IntroductionUpdateVo IntroRespVo) {
        SingleIntroduction introduction =IntroductionMapper.selectById(IntroRespVo.getId());
        introduction.setOpen(IntroRespVo.getOpen());
        introduction.setTypeName(IntroRespVo.getTypeName());
        IntroductionMapper.updateById(introduction);
        return JsonResult.success("修改成功-"+IntroRespVo.getId());
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public JsonResult updateIntroduction(IntroductionUpdateVo introResp) throws IOException {

        JsonResult result=null;

        SingleIntroduction introduction =IntroductionMapper.selectById(introResp.getId());
        introduction.setContent(introResp.getContent());
        introduction.setTypeName(introResp.getTypeName());
        IntroductionMapper.updateById(introduction);

        Query query = new Query(Criteria.where("introId").is(introResp.getId()));
        UploadFile file = mongoTemplate.findOne(query,UploadFile.class);
        if(file!=null){ //更新
            file.setContent(new Binary(introResp.getPicFile().getBytes()));
            file.setName(introResp.getPicFile().getOriginalFilename());
            file.setUpdateTime(new Date());
            file.setIntroId(introResp.getId());
            file.setContentType(introResp.getPicFile().getContentType());
            file.setSize(introResp.getPicFile().getSize());
            mongoTemplate.save(file);
            result = JsonResult.success("简介id-> "+introResp.getId()+",照片更新");
        }else{  //新增
            UploadFile uploadFile = new UploadFile();
            uploadFile.setContent(new Binary(introResp.getPicFile().getBytes()));
            uploadFile.setName(introResp.getPicFile().getOriginalFilename());
            uploadFile.setCreatedTime(new Date());
            uploadFile.setIntroId(introResp.getId());//introId
            uploadFile.setContentType(introResp.getPicFile().getContentType());
            uploadFile.setSize(introResp.getPicFile().getSize());
            mongoTemplate.save(uploadFile);
            result = JsonResult.success("简介id-> "+introResp.getId()+",照片新增");
        }

        return result;
    }

    @Override
    public JsonResult deleteIntroduction(Integer introId) {
        IntroductionMapper.deleteById(introId);
        return JsonResult.success("删除成功 -> "+introId);
    }
}
